
/*
 * GET home page.
 */
var crypto = require('crypto');
var User = require('../models/user.js')


module.exports = function(app) {
	app.get('/', function(req, res) {
		res.render('index', {title: '首页'});
	});
	app.get('/reg', function(req, res) {
		res.render('reg', {title: '用户注册'});
	});
	app.get('/radica', function(req,res){
		res.render('radica', {title: 'radica ui'});
	});
	app.get('/jstest', function(req,res){
		res.render('jstest', {title: 'jstest'});
	});
	app.post('/reg', function(req, res) {
		//检验用户两次输入的口令是否一致
		if (req.body['password-repeat'] != req.body['password']) {
			req.flash('error', '两次输入的口令不一致');
			console.log("两次输入的口令不一致");
			return res.redirect('/reg');
		}
		//生成口令的散列值
		var md5 = crypto.createHash('md5');
		var password = md5.update(req.body.password).digest('base64');

		var user = {
			name: req.body.username,
			password: password,
		};
		var newUser = new User();
		newUser.save(user);
		res.redirect('/');

	});


};
